package org.hiucung.localmusicdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import org.hiucung.localmusicdemo.bean.LocalMusicBean;
import org.hiucung.localmusicdemo.R;

import java.util.ArrayList;

public class SearchMusicAdapter extends RecyclerView.Adapter<SearchMusicAdapter.SearchMusicViewHolder> {

    Context context;
    ArrayList<LocalMusicBean> mDatas;

    OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(SearchMusicAdapter.OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    //通过接口回调的方式
    public interface OnItemClickListener{
        public void OnItemClick(View view,int position);
    }

    public SearchMusicAdapter() {

    }

    public SearchMusicAdapter(Context context, ArrayList<LocalMusicBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @NonNull
    @Override
    public SearchMusicViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_search_music,parent,false);
        return new SearchMusicViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchMusicViewHolder holder, int position) {
        LocalMusicBean musicBean = mDatas.get(position);
        holder.songTv.setText(musicBean.getSong());
        holder.singerTv.setText(musicBean.getSinger());

        //通过glide获取专辑封面
        Glide.with(context).load(mDatas.get(position).getAlbumUrl()).into(holder.ivCover);
        //可以通过itemiew获取每一项的歌曲
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.OnItemClick(v,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    static class SearchMusicViewHolder extends RecyclerView.ViewHolder{

        TextView singerTv,songTv;
        ImageView musicTag,ivCover;
        public SearchMusicViewHolder(@NonNull View itemView) {
            super(itemView);
            songTv = itemView.findViewById(R.id.tvTitle);
            singerTv = itemView.findViewById(R.id.tvSub);
            ivCover = itemView.findViewById(R.id.ivCover);
        }
    }
}

